00001 /* 00002 * Copyright (c) 2013 Battelle Memorial Institute 00003 * Licensed under modified BSD License. A copy of this license can be found 00004 * in the LICENSE file in the top level directory of this distribution. 00005 */ 00006 // ------------------------------------------------------------- 00007 /** 00008 * @file se_components.hpp 00009 * @author Yousu Chen 00010 * @date 2016-07-14 13:37:55 d3g096 00011 * 00012 * @brief 00013 * 00014 * 00015 */ 00016 // ------------------------------------------------------------- 00017 00018 #ifndef _se_components_h_ 00019 #define _se_components_h_ 00020 00021 #include "boost/smart_ptr/shared_ptr.hpp" 00022 #include "gridpack/utilities/complex.hpp" 00023 #include "gridpack/component/base_component.hpp" 00024 #include "gridpack/component/data_collection.hpp" 00025 #include "gridpack/network/base_network.hpp" 00026 #include "gridpack/applications/components/y_matrix/ymatrix_components.hpp" 00027 00028 namespace gridpack { 00029 namespace state_estimation{ 00030 00031 enum SEMode{YBus,Jacobian_H, R_inv, Ez, Voltage}; 00032 00033 struct Measurement 00034 { 00035 char p_type[4]; 00036 //std::string p_type; 00037 int p_busid; 00038 int p_fbusid; 00039 int p_tbusid; 00040 //std::string p_ckt; 00041 char p_ckt[3]; 00042 double p_value; 00043 double p_deviation; 00044 }; 00045 00046 class SEBus 00047 : public gridpack::ymatrix::YMBus 00048 { 00049 public: 00050 /** 00051 * Simple constructor 00052 */ 00053 SEBus(void); 00054 00055 /** 00056 * Simple destructor 00057 */ 00058 ~SEBus(void); 00059 00060 /** 00061 * Return size of matrix block on the diagonal contributed by 00062 * component 00063 * @param isize, jsize: number of rows and columns of matrix block 00064 * @return: false if network component does not contribute matrix 00065 * element 00066 */ 00067 bool matrixDiagSize(int *isize, int *jsize) const; 00068 00069 /** 00070 * Return the values of for a diagonal matrix block. The values are 00071 * returned in row-major order 00072 * @param values: pointer to matrix block values 00073 * @return: false if network component does not contribute 00074 * matrix element 00075 */ 00076 bool matrixDiagValues(ComplexType *values); 00077 00078 /** 00079 * Return size of vector block contributed by component 00080 * @param isize: number of vector elements 00081 * @return: false if network component does not contribute 00082 * vector element 00083 */ 00084 bool vectorSize(int *isize) const; 00085 00086 /** 00087 * Return the values of the vector block 00088 * @param values: pointer to vector values 00089 * @return: false if network component does not contribute 00090 * vector element 00091 */ 00092 bool vectorValues(ComplexType *values); 00093 00094 /** 00095 * Set the internal values of the voltage magnitude and phase angle. Need this 00096 * function to push values from vectors back onto buses 00097 * @param values array containing voltage magnitude and angle 00098 */ 00099 void setValues(gridpack::ComplexType *values); 00100 00101 /** 00102 * Return number of elements in vector coming from component 00103 * @return number of elements contributed from component 00104 */ 00105 int vectorNumElements() const; 00106 00107 /** 00108 * Set indices corresponding to the elements contributed by this 00109 * component 00110 * @param ielem index of element contributed by this component (e.g. 00111 * if component contributes 3 elements then ielem is between 0 and 2) 00112 * @param idx vector index of element ielem 00113 */ 00114 void vectorSetElementIndex(int ielem, int idx); 00115 00116 /** 00117 * Get list of element indices from component 00118 * @param idx list of indices that component maps onto 00119 */ 00120 void vectorGetElementIndices(int *idx); 00121 00122 /** 00123 * Get a list of vector values contributed by this component and their 00124 * indices 00125 * @param values list of vector element values 00126 * @param idx indices for the vector elements 00127 */ 00128 void vectorGetElementValues(ComplexType *values, int *idx); 00129 00130 /** 00131 * Transfer vector values to component 00132 * @param values list of vector element values 00133 */ 00134 void vectorSetElementValues(ComplexType *values); 00135 00136 /** 00137 * Return the size of the buffer used in data exchanges on the network. 00138 * For this problem, the voltage magnitude and phase angle need to be exchanged 00139 * @return size of buffer 00140 */ 00141 int getXCBufSize(void); 00142 00143 /** 00144 * Assign pointers for voltage magnitude and phase angle 00145 */ 00146 void setXCBuf(void *buf); 00147 00148 /** 00149 * Set values of YBus matrix. These can then be used in subsequent 00150 * calculations 00151 */ 00152 void setYBus(void); 00153 00154 /** 00155 * Get values of YBus matrix. These can then be used in subsequent 00156 * calculations 00157 */ 00158 gridpack::ComplexType getYBus(void); 00159 00160 /** 00161 * Load values stored in DataCollection object into SEBus object. The 00162 * DataCollection object will have been filled when the network was created 00163 * from an external configuration file 00164 * @param data: DataCollection object contain parameters relevant to this 00165 * bus that were read in when network was initialized 00166 */ 00167 void load(const boost::shared_ptr<gridpack::component::DataCollection> &data); 00168 00169 /** 00170 * Set the mode to control what matrices and vectors are built when using 00171 * the mapper 00172 * @param mode: enumerated constant for different modes 00173 */ 00174 void setMode(int mode); 00175 00176 /** 00177 * Return the value of the voltage magnitude on this bus 00178 * @return voltage magnitude 00179 */ 00180 double getVoltage(void); 00181 00182 /** 00183 * Return the complex voltage on this bus 00184 * @return the complex voltage 00185 */ 00186 ComplexType getComplexVoltage(void); 00187 00188 /** 00189 * Return the value of the phase angle on this bus 00190 * @return: phase angle 00191 */ 00192 double getPhase(void); 00193 00194 /** 00195 * Return whether or not the bus is a PV bus (V held fixed in powerflow 00196 * equations) 00197 * @return true if bus is PV bus 00198 */ 00199 bool isPV(void); 00200 00201 /** 00202 * Return whether or not a bus is isolated 00203 * @return true if bus is isolated 00204 */ 00205 bool isIsolated(void) const; 00206 00207 /** 00208 * Write output from buses to standard out 00209 * @param string (output) string with information to be printed out 00210 * @param bufsize size of string buffer in bytes 00211 * @param signal an optional character string to signal to this 00212 * routine what about kind of information to write 00213 * @return true if bus is contributing string to output, false otherwise 00214 */ 00215 bool serialWrite(char *string, const int bufsize, const char *signal = NULL); 00216 00217 /** 00218 * Add a measurement to the bus 00219 * @param measurement a measurement struct that will be used to assign 00220 * internal paramters 00221 */ 00222 void addMeasurement(Measurement measurement); 00223 00224 /** 00225 * Sort measurements so that they are in a consistent order 00226 */ 00227 void sortMeasurements(void); 00228 00229 /** 00230 * Return number of rows in matrix from component 00231 * @return number of rows from component 00232 */ 00233 int matrixNumRows() const; 00234 00235 /** 00236 * Return number of columns in matrix from component 00237 * @return number of columnsows from component 00238 */ 00239 int matrixNumCols() const; 00240 00241 /** 00242 * Set row indices corresponding to the rows contributed by this 00243 * component 00244 * @param irow index of row contributed by this component (e.g. if component 00245 * contributes 3 rows then irow is between 0 and 2) 00246 * @param idx matrix index of row irow 00247 */ 00248 void matrixSetRowIndex(int irow, int idx); 00249 00250 /** 00251 * Set column indices corresponding to the columns contributed by this 00252 * component 00253 * @param icol index of column contributed by this component (e.g. if component 00254 * contributes 3 columns then icol is between 0 and 2) 00255 * @param idx matrix index of column icol 00256 */ 00257 void matrixSetColIndex(int icol, int idx); 00258 00259 /** 00260 * Get the row index corresponding to the rows contributed by this component 00261 * @param irow index of row contributed by this component (e.g. if component 00262 * contributes 3 rows then irow is between 0 and 2) 00263 * @return matrix index of row irow 00264 */ 00265 int matrixGetRowIndex(int idx); 00266 00267 /** 00268 * Get the column index corresponding to the columns contributed by this component 00269 * @param icol index of column contributed by this component (e.g. if component 00270 * contributes 3 columns then icol is between 0 and 2) 00271 * @return matrix index of column icol 00272 */ 00273 int matrixGetColIndex(int idx); 00274 00275 /** 00276 * Return the number of matrix values contributed by this component 00277 * @return number of matrix values 00278 */ 00279 int matrixNumValues() const; 00280 00281 /** 00282 * Return values from a matrix block 00283 * @param values: pointer to matrix block values 00284 * @param rows: pointer to matrix block rows 00285 * @param cols: pointer to matrix block cols 00286 */ 00287 void matrixGetValues(ComplexType *values, int *rows, int *cols); 00288 00289 /** 00290 * Configure buses with state estimation parameters. These can be used in 00291 * other methods 00292 */ 00293 void configureSE(void); 00294 00295 /** 00296 * Save state variables inside the component to a DataCollection object. 00297 * This can be used as a way of moving data in a way that is useful for 00298 * creating output or for copying state data from one network to another. 00299 * @param data data collection object into which new values are inserted 00300 */ 00301 void saveData(boost::shared_ptr<gridpack::component::DataCollection> data); 00302 00303 /** 00304 * Get shunt_gs and shunt_bs 00305 * 00306 */ 00307 void getShuntGsBs(double *gs, double *bs); 00308 00309 00310 private: 00311 double p_shunt_gs; 00312 double p_shunt_bs; 00313 bool p_shunt; 00314 bool p_load; 00315 int p_mode; 00316 00317 // p_v and p_a are initialized to p_voltage and p_angle respectively, 00318 // but may be subject to change during the NR iterations 00319 double p_v, p_a; 00320 double p_theta; //phase angle difference 00321 double p_ybusr, p_ybusi; 00322 double p_P0, p_Q0; //double p_sbusr, p_sbusi; 00323 double p_angle; // initial bus angle read from parser 00324 double p_voltage; // initial bus voltage read from parser 00325 // newly added priavate variables: 00326 std::vector<double> p_pg, p_qg; 00327 std::vector<int> p_gstatus; 00328 std::vector<double> p_vs; 00329 std::vector<double> p_qmin; 00330 std::vector<double> p_qmax; 00331 std::vector<int> p_gid; 00332 double p_pl, p_ql; 00333 double p_sbase; 00334 double p_Pinj, p_Qinj; 00335 bool p_isPV; 00336 int p_numElements; 00337 std::vector<int> p_colJidx; 00338 std::vector<int> p_rowJidx; 00339 std::vector<int> p_colRidx; 00340 std::vector<int> p_rowRidx; 00341 std::vector<int> p_vecZidx; 00342 std::vector<Measurement> p_meas; 00343 00344 /** 00345 * Variables that are exchanged between buses 00346 */ 00347 double* p_vMag_ptr; 00348 double* p_vAng_ptr; 00349 00350 private: 00351 00352 00353 friend class boost::serialization::access; 00354 00355 template<class Archive> 00356 void serialize(Archive & ar, const unsigned int version) 00357 { 00358 // ar & boost::serialization::base_object<gridpack::component::BaseBusComponent>(*this) 00359 ar & boost::serialization::base_object<gridpack::ymatrix::YMBus>(*this) 00360 & p_shunt_gs 00361 & p_shunt_bs 00362 & p_shunt 00363 & p_load 00364 & p_mode 00365 & p_v & p_a & p_theta 00366 & p_ybusr & p_ybusi 00367 & p_P0 & p_Q0 00368 & p_angle & p_voltage 00369 & p_pg & p_qg 00370 & p_gstatus 00371 & p_vs & p_gid 00372 & p_pl & p_ql 00373 & p_sbase 00374 & p_Pinj & p_Qinj 00375 & p_isPV 00376 & p_numElements 00377 & p_colJidx 00378 & p_rowJidx 00379 & p_colRidx 00380 & p_rowRidx 00381 & p_vecZidx; 00382 } 00383 00384 }; 00385 00386 class SEBranch 00387 : public gridpack::ymatrix::YMBranch { 00388 public: 00389 /** 00390 * Simple constructor 00391 */ 00392 SEBranch(void); 00393 00394 /** 00395 * Simple destructor 00396 */ 00397 ~SEBranch(void); 00398 00399 /** 00400 * Return size of off-diagonal matrix block contributed by the component 00401 * for the forward/reverse directions 00402 * @param isize, jsize: number of rows and columns of matrix block 00403 * @return: false if network component does not contribute matrix element 00404 */ 00405 bool matrixForwardSize(int *isize, int *jsize) const; 00406 bool matrixReverseSize(int *isize, int *jsize) const; 00407 00408 /** 00409 * Return the values of the forward/reverse matrix block. The values are 00410 * returned in row-major order 00411 * @param values: pointer to matrix block values 00412 * @return: false if network component does not contribute matrix element 00413 */ 00414 bool matrixForwardValues(ComplexType *values); 00415 bool matrixReverseValues(ComplexType *values); 00416 00417 /** 00418 * Set values of YBus matrix. These can then be used in subsequent 00419 * calculations 00420 */ 00421 void setYBus(void); 00422 00423 /** 00424 * Get values of YBus matrix. These can then be used in subsequent 00425 * calculations 00426 */ 00427 gridpack::ComplexType getYBus(void); 00428 00429 /** 00430 * Load values stored in DataCollection object into SEBranch object. The 00431 * DataCollection object will have been filled when the network was created 00432 * from an external configuration file 00433 * @param data: DataCollection object contain parameters relevant to this 00434 * branch that were read in when network was initialized 00435 */ 00436 void load(const boost::shared_ptr<gridpack::component::DataCollection> &data); 00437 00438 /** 00439 * Return the complex admittance of the branch 00440 * @return: complex addmittance of branch 00441 */ 00442 gridpack::ComplexType getAdmittance(void); 00443 00444 /** 00445 * Return transformer contribution from the branch to the calling 00446 * bus 00447 * @param bus: pointer to the bus making the call 00448 * @return: contribution from transformers to Y matrix 00449 */ 00450 gridpack::ComplexType getTransformer(SEBus *bus); 00451 00452 /** 00453 * Return the contribution to a bus from shunts 00454 * @param bus: pointer to the bus making the call 00455 * @return: contribution to Y matrix from shunts associated with branches 00456 */ 00457 gridpack::ComplexType getShunt(SEBus *bus); 00458 00459 /** 00460 * Set the mode to control what matrices and vectors are built when using 00461 * the mapper 00462 * @param mode: enumerated constant for different modes 00463 */ 00464 void setMode(int mode); 00465 00466 /** 00467 * Write output from branches to standard out 00468 * @param string (output) string with information to be printed out 00469 * @param bufsize size of string buffer in bytes 00470 * @param signal an optional character string to signal to this 00471 * routine what about kind of information to write 00472 * @return true if branch is contributing string to output, false otherwise 00473 */ 00474 bool serialWrite(char *string, const int bufsize, const char *signal = NULL); 00475 00476 /** 00477 ** Return complex power for line element 00478 ** @param tag describing line element on branch 00479 ** @return complex power 00480 **/ 00481 gridpack::ComplexType getComplexPower(std::string tag); 00482 00483 /** 00484 ** Return complex power for line element at to end 00485 ** @param tag describing line element on branch 00486 ** @return complex power 00487 **/ 00488 gridpack::ComplexType getRvrsComplexPower(std::string tag); 00489 00490 /** 00491 * Add a measurement to the branch 00492 * @param measurement a measurement struct that will be used to assign 00493 * internal paramters 00494 */ 00495 void addMeasurement(Measurement measurement); 00496 00497 /** 00498 * Sort measurements so that they are in a consistent order 00499 */ 00500 void sortMeasurements(void); 00501 00502 /** 00503 * Return contribution to constraints 00504 * @param v: voltage at the other bus 00505 * @param theta: angle difference between two buses 00506 */ 00507 void getVTheta(gridpack::state_estimation::SEBus *bus, double *v, double *theta); 00508 00509 /** 00510 * Return contribution to constraints 00511 * @param v1, v2: voltages at buses 00512 * @param theta: angle difference between two buses 00513 */ 00514 void getV1V2Theta(gridpack::state_estimation::SEBranch *branch, double *v1, double *v2, double *theta); 00515 00516 /** 00517 * Return contribution to constraints 00518 * @param p: real part of constraint 00519 * @param q: imaginary part of constraint 00520 */ 00521 void getPQ(SEBus *bus, double *p, double *q); 00522 00523 /** 00524 * Return number of rows in matrix from component 00525 * @return number of rows from component 00526 */ 00527 int matrixNumRows() const; 00528 00529 /** 00530 * Return number of columns in matrix from component 00531 * @return number of columnsows from component 00532 */ 00533 int matrixNumCols() const; 00534 00535 /** 00536 * Set row indices corresponding to the rows contributed by this 00537 * component 00538 * @param irow index of row contributed by this component (e.g. if component 00539 * contributes 3 rows then irow is between 0 and 2) 00540 * @param idx matrix index of row irow 00541 */ 00542 void matrixSetRowIndex(int irow, int idx); 00543 00544 /** 00545 * Set column indices corresponding to the columns contributed by this 00546 * component 00547 * @param icol index of column contributed by this component (e.g. if component 00548 * contributes 3 columns then icol is between 0 and 2) 00549 * @param idx matrix index of column icol 00550 */ 00551 void matrixSetColIndex(int icol, int idx); 00552 00553 /** 00554 * Get the row index corresponding to the rows contributed by this component 00555 * @param irow index of row contributed by this component (e.g. if component 00556 * contributes 3 rows then irow is between 0 and 2) 00557 * @return matrix index of row irow 00558 */ 00559 int matrixGetRowIndex(int idx); 00560 00561 /** 00562 * Get the column index corresponding to the columns contributed by this component 00563 * @param icol index of column contributed by this component (e.g. if component 00564 * contributes 3 columns then icol is between 0 and 2) 00565 * @return matrix index of column icol 00566 */ 00567 int matrixGetColIndex(int idx); 00568 00569 /** 00570 * Return the number of matrix values contributed by this component 00571 * @return number of matrix values 00572 */ 00573 int matrixNumValues() const; 00574 00575 /** 00576 * Return values from a matrix block 00577 * @param values: pointer to matrix block values 00578 * @param rows: pointer to matrix block rows 00579 * @param cols: pointer to matrix block cols 00580 */ 00581 void matrixGetValues(ComplexType *values, int *rows, int *cols); 00582 00583 /** 00584 * Return number of elements in vector coming from component 00585 * @return number of elements contributed from component 00586 */ 00587 int vectorNumElements() const; 00588 00589 /** 00590 * Set indices corresponding to the elements contributed by this 00591 * component 00592 * @param ielem index of element contributed by this component (e.g. 00593 * if component contributes 3 elements then ielem is between 0 and 2) 00594 * @param idx vector index of element 00595 * ielem 00596 */ 00597 void vectorSetElementIndex(int ielem, int idx); 00598 00599 /** 00600 * Get list of element indices from component 00601 * @param idx list of indices that component maps onto 00602 */ 00603 void vectorGetElementIndices(int *idx); 00604 00605 /** 00606 * Get a list of vector values contributed by this component and their 00607 * indices 00608 * @param values list of vector element values 00609 * @param idx indices for the vector elements 00610 */ 00611 void vectorGetElementValues(ComplexType *values, int *idx); 00612 00613 /** 00614 * Transfer vector values to component 00615 * @param values list of vector element values 00616 */ 00617 void vectorSetElementValues(ComplexType *values); 00618 00619 /** 00620 * Configure branches with state estimation parameters. These can be used in 00621 * other methods 00622 */ 00623 void configureSE(void); 00624 00625 private: 00626 std::vector<double> p_reactance; 00627 std::vector<double> p_resistance; 00628 std::vector<double> p_tap_ratio; 00629 std::vector<double> p_phase_shift; 00630 std::vector<double> p_charging; 00631 std::vector<double> p_shunt_admt_g1; 00632 std::vector<double> p_shunt_admt_b1; 00633 std::vector<double> p_shunt_admt_g2; 00634 std::vector<double> p_shunt_admt_b2; 00635 std::vector<bool> p_xform, p_shunt; 00636 int p_mode; 00637 double p_ybusr_frwd, p_ybusi_frwd; 00638 double p_ybusr_rvrs, p_ybusi_rvrs; 00639 double p_theta; 00640 double p_sbase; 00641 double p_shunt_bs; 00642 double p_shunt_gs; 00643 std::vector<bool> p_branch_status; 00644 std::vector<std::string> p_tag; 00645 int p_elems; 00646 bool p_active; 00647 int p_numElements; 00648 std::vector<int> p_colJidx; 00649 std::vector<int> p_rowJidx; 00650 std::vector<int> p_colRidx; 00651 std::vector<int> p_rowRidx; 00652 std::vector<int> p_vecZidx; 00653 std::vector<Measurement> p_meas; 00654 00655 private: 00656 00657 00658 friend class boost::serialization::access; 00659 00660 template<class Archive> 00661 void serialize(Archive & ar, const unsigned int version) 00662 { 00663 // ar & boost::serialization::base_object<gridpack::component::BaseBranchComponent>(*this) 00664 ar & boost::serialization::base_object<gridpack::ymatrix::YMBranch>(*this) 00665 & p_reactance 00666 & p_resistance 00667 & p_tap_ratio 00668 & p_phase_shift 00669 & p_charging 00670 & p_shunt_admt_g1 00671 & p_shunt_admt_b1 00672 & p_shunt_admt_g2 00673 & p_shunt_admt_b2 00674 & p_xform & p_shunt 00675 & p_shunt_gs & p_shunt_bs 00676 & p_mode 00677 & p_ybusr_frwd & p_ybusi_frwd 00678 & p_ybusr_rvrs & p_ybusi_rvrs 00679 & p_theta 00680 & p_sbase 00681 & p_branch_status 00682 & p_tag 00683 & p_elems 00684 & p_active 00685 & p_numElements 00686 & p_colJidx 00687 & p_rowJidx 00688 & p_colRidx 00689 & p_rowRidx 00690 & p_vecZidx; 00691 } 00692 00693 }; 00694 00695 00696 /// The type of network used in the contingency analysis application 00697 typedef network::BaseNetwork<SEBus, SEBranch > SENetwork; 00698 00699 00700 } // state_estimation 00701 } // gridpack 00702 00703 BOOST_CLASS_EXPORT_KEY(gridpack::state_estimation::SEBus) 00704 BOOST_CLASS_EXPORT_KEY(gridpack::state_estimation::SEBranch) 00705 00706 00707 #endif